<template>
  <view class="caption">
    <text>猜你喜欢</text>
  </view>

  <view class="love">
    <view v-for="item in GuessLikeData" class="loveBox">
      <navigator hover-class="none" :url="`/pages/hot/hot?type=`" class="Card">
        <image class="CardImgItem" :src="item.picture" mode="aspectFit"> </image>
        <view class="name">
          {{ item.desc }}
        </view>
        <view class="price"> <text style="font-size: 90%">＄</text>{{ item.price }} </view>
      </navigator>
    </view>
  </view>
  <view class="loading-text">
    {{ finish?"没有更多数据了":"正在加载...." }}
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { GuessLikeInValues, GuessLikeItem, GuessLikePageParams } from "../APi/types";
import { getGuessLike } from "../APi";import { onLoad } from "@dcloudio/uni-app";

let pageParams: Required<GuessLikePageParams> = {
  page: 1,
  pageSize: 10,
};
let finish = ref(false);
const GuessLikeData = ref<GuessLikeInValues[]>([]);
const getIndexGuessLikeData = async () => {
    console.log("触发");
    
  if (finish.value == true) {
    return uni.showToast({
      icon: "none",
      title: "没有更多数据了。",
    });
  }
  const res = await getGuessLike(pageParams);
  console.log(res);
  
  GuessLikeData.value.push(...res.result.items);
  if (pageParams.page < res.result.pages) {
    pageParams.page++;
    console.log("页数添加");
    
  } else {
    finish.value = true;
  }
};
const resetData = () => {
  pageParams.page = 1; // 分号分隔，避免连续赋值问题
  GuessLikeData.value = [];
  finish.value = false;
};
defineExpose({
    resetData,
    getGuessLike:getIndexGuessLikeData
})
// onLoad 是 页面级生命周期，只能在 pages 目录下的页面文件中触发，组件里用 onLoad 完全不会执行 —— 这就是 “猜你喜欢” 没数据的核心原因！
onMounted (() => {
    getIndexGuessLikeData()
     console.log("子组件挂载，开始加载数据");
})
</script>

<style lang="scss">
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 32rpx;
  color: #262626;
  text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: "";
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
}
.love {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx;
  .loveBox {
    .Card {
      width: 300rpx;
      padding: 24rpx 20rpx 20rpx;
      margin-bottom: 20rpx;
      border-radius: 10rpx;
      overflow: hidden;
      background-color: #fff;
      .CardImgItem {
        width: 300rpx;
        height: 300rpx;
      }
      .name {
        height: 75rpx;
        margin: 10rpx 0;
        font-size: 26rpx;
        color: #262626;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      .price {
        line-height: 1;
        padding-top: 4rpx;
        color: #cf4444;
        font-size: 26rpx;
      }
    }
  }
}
</style>
